package com.ruoyi.work.service.impl;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.MapExcelUtil;
import com.ruoyi.work.domain.vo.UserHourByDateReport;
import com.ruoyi.work.domain.vo.UserHourByDeptReport;
import com.ruoyi.work.domain.vo.UserHourReport;
import com.ruoyi.work.service.IWorkReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: MMDZ
 * @Desc: TODO 人员工时清单-导出
 */
@Service
public class UserHortExportServiceImpl {

    @Autowired
    private IWorkReportService workReportService;

    /**
     * @param response
     * @param report
     */
    public void export(HttpServletResponse response, UserHourReport report) {
        Map map = workReportService.selectUserHourList(report, 1, 10, true);//导出数据

        String tabKey = report.getParams().get("TabKey").toString();
        // 获取Tab页
        if (Objects.isNull(tabKey) || StringUtils.isEmpty(tabKey)) {
            throw new ServiceException("获取人员工时清单参数为空，请联系管理员", HttpStatus.ERROR);
        }

        switch (tabKey) {
            case "first"://数据汇总
                exportAllSum(response, map);
                break;
            case "second"://部门合计
                exportDeptSum(response, map);
                break;
            case "third"://日期总计
                exportDateSum(response, map);
                break;
            case "fourth"://项目合计
                exportAllSum(response, map);
                break;
            default:
                throw new ServiceException("人员工时清单数据错误，请联系管理员", HttpStatus.ERROR);

        }
    }

    /**
     * 数据总汇导出
     */
    private void exportAllSum(HttpServletResponse response, Map resMap) {
        List<String> projectCodes = (List<String>) resMap.get("projectCodes");
        List<UserHourReport> list = (List<UserHourReport>) resMap.get("data");

        if (Objects.isNull(projectCodes) || Objects.isNull(list)) {
            throw new ServiceException("总汇合计导出数据异常，请联系管理员", HttpStatus.ERROR);
        }

        List<String> heads = new ArrayList<>();// 标题
        List<String> tempHeads = new ArrayList<>();// 数据 key
        // 标题数据
        List<String> head = Stream.of("部门", "姓名", "工号", "岗位性质", "日期", "项目合计工时(H)").collect(Collectors.toList());
        List<String> tempHead = Stream.of("deptName", "nickName", "userName", "postName", "strDate", "sumProjectHour").collect(Collectors.toList());
        // 添加标题数据 key
        tempHeads.addAll(tempHead);
        tempHeads.addAll(projectCodes);
        // 添加标题
        heads.addAll(head);
        heads.addAll(projectCodes);

        // 添加数据，转换格式
        List<Map<String, Object>> dataMapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("deptName", list.get(i).getDeptName());//部门
            dataMap.put("nickName", list.get(i).getNickName());//姓名
            dataMap.put("userName", list.get(i).getUserName());//工号
            dataMap.put("postName", list.get(i).getPostName());//岗位性质
            dataMap.put("strDate", list.get(i).getStrDate());//日期
            dataMap.put("sumProjectHour", list.get(i).getSumProjectHour());//项目合计工时
            List<Map<String, Double>> proList = list.get(i).getProjectHours();//项目代码
            proList.forEach(map -> dataMap.putAll(map));
            dataMapList.add(dataMap);
        }

        MapExcelUtil.exportExcel(response, heads, tempHeads, dataMapList, "总汇信息数据");
    }

    /**
     * 部门合计导出
     */
    private void exportDeptSum(HttpServletResponse response, Map resMap) {
        List<String> projectCodes = (List<String>) resMap.get("projectCodes");
        List<UserHourByDeptReport> list = (List<UserHourByDeptReport>) resMap.get("data");

        if (Objects.isNull(projectCodes) || Objects.isNull(list)) {
            throw new ServiceException("部门合计导出数据异常，请联系管理员", HttpStatus.ERROR);
        }

        List<String> heads = new ArrayList<>();// 标题
        List<String> tempHeads = new ArrayList<>();// 数据 key
        // 标题数据
        List<String> head = Stream.of("部门", "日期", "项目合计工时(H)").collect(Collectors.toList());
        List<String> tempHead = Stream.of("deptName", "strDate", "sumProjectHour").collect(Collectors.toList());
        // 添加标题数据 key
        tempHeads.addAll(tempHead);
        tempHeads.addAll(projectCodes);
        // 添加标题
        heads.addAll(head);
        heads.addAll(projectCodes);

        // 添加数据，转换格式
        List<Map<String, Object>> dataMapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("deptName", list.get(i).getDeptName());//部门
            dataMap.put("strDate", list.get(i).getStrDate());//日期
            dataMap.put("sumProjectHour", list.get(i).getSumProjectHour());//项目合计工时
            List<Map<String, Double>> proList = list.get(i).getProjectHours();//项目代码
            proList.forEach(map -> dataMap.putAll(map));
            dataMapList.add(dataMap);
        }

        MapExcelUtil.exportExcel(response, heads, tempHeads, dataMapList, "部门合计信息数据");
    }

    /**
     * 日期合计导出
     */
    private void exportDateSum(HttpServletResponse response, Map resMap) {
        List<String> projectCodes = (List<String>) resMap.get("projectCodes");
        List<UserHourByDateReport> list = (List<UserHourByDateReport>) resMap.get("data");

        if (Objects.isNull(projectCodes) || Objects.isNull(list)) {
            throw new ServiceException("日期合计导出数据异常，请联系管理员", HttpStatus.ERROR);
        }

        List<String> heads = new ArrayList<>();// 标题
        List<String> tempHeads = new ArrayList<>();// 数据 key
        // 标题数据
        List<String> head = Stream.of("日期", "项目合计工时(H)").collect(Collectors.toList());
        List<String> tempHead = Stream.of("strDate", "sumProjectHour").collect(Collectors.toList());
        // 添加标题数据 key
        tempHeads.addAll(tempHead);
        tempHeads.addAll(projectCodes);
        // 添加标题
        heads.addAll(head);
        heads.addAll(projectCodes);

        // 添加数据，转换格式
        List<Map<String, Object>> dataMapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("strDate", list.get(i).getStrDate());//日期
            dataMap.put("sumProjectHour", list.get(i).getSumProjectHour());//项目合计工时
            List<Map<String, Double>> proList = list.get(i).getProjectHours();//项目代码
            proList.forEach(map -> dataMap.putAll(map));
            dataMapList.add(dataMap);
        }

        MapExcelUtil.exportExcel(response, heads, tempHeads, dataMapList, "部门合计信息数据");
    }
}